package com.xidian.zhiancloud.service.equipFacility;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xidian.zhiancloud.constant.EquipStatus;
import com.xidian.zhiancloud.model.DeviceType;
import com.xidian.zhiancloud.model.EqFacMng;
import com.xidian.zhiancloud.model.OhRegister;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysPosition;
import com.xidian.zhiancloud.qo.equipFacility.StatisticQO;
import com.xidian.zhiancloud.service.systemSetting.SysPositionService;
import com.xidian.zhiancloud.service.user.usermanage.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 　　* @program zhiancloud
 * 　　* @author Egoist
 * 　　* @create 2021/4/15 16:26
 *
 */
@Service
public class EquipStatisticsService {

    @Autowired
    private OhRegisterService ohRegisterService;
    @Autowired
    private EqFacMngService eqFacMngService;
    @Autowired
    private SysPositionService positionService;
    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private DeviceTypeService deviceTypeService;

    private List<EqFacMng>  getEquipList(StatisticQO qo) {
        LambdaQueryWrapper<EqFacMng> query = new LambdaQueryWrapper<EqFacMng>();
        query.eq(EqFacMng::getCompanyId, qo.getCompanyId());
        if (qo.getDeviceStatus() != null) {
            query.eq(EqFacMng::getEquipStatus, qo.getDeviceStatus());
        }
        if (qo.getOrganizationId() != null) {
            query.eq(EqFacMng::getMngCtrDeptId, qo.getOrganizationId());
        }
        if (qo.getPositionId() != null) {
            query.eq(EqFacMng::getPosition, qo.getPositionId());
        }
        if (qo.getTypeId() != null) {
            query.eq(EqFacMng::getDeviceTypeId, qo.getTypeId());
        }
        return eqFacMngService.list(query);
    }


    public Map<Long, Map> getPositionStatistics(StatisticQO qo) {
        Map<Long, Map> map = new HashMap<>();
        List<EqFacMng> eqFacMngList = getEquipList(qo);
        for (EqFacMng eqFacMng : eqFacMngList) {
            Long positionId = eqFacMng.getPosition();
            Integer special = eqFacMng.getChartEq();
            int ordinary = 0;
            if (special == 0) {
                ordinary = 1;
            }
            Map quantityInfo = new HashMap<>();
            if (map.get(positionId) == null) {
                quantityInfo.put("total", 1);
                quantityInfo.put("ordinary", ordinary);
                quantityInfo.put("special", special);
            } else {
                Map<String, Integer> lastInfo = map.get(positionId);
                quantityInfo.put("total", lastInfo.get("total") + 1);
                quantityInfo.put("ordinary", lastInfo.get("ordinary") + ordinary);
                quantityInfo.put("special", lastInfo.get("special") + special);
            }
            SysPosition position = positionService.getById(positionId);
            quantityInfo.put("postition", position == null ? "该位置名称已删除!" : position.getName() );
            map.put(positionId, quantityInfo);
        }
        return map;
    }


    public Map<String,Integer> getEquipQuantity(StatisticQO qo) {
        Map<String,Integer> map = new HashMap<>();
        LambdaQueryWrapper<EqFacMng> query = new LambdaQueryWrapper<EqFacMng>();
        query.eq(EqFacMng::getCompanyId, qo.getCompanyId());
        int total = eqFacMngService.count(query);
        query.eq(EqFacMng::getChartEq, 1);
        int special = eqFacMngService.count(query);
        LambdaQueryWrapper<OhRegister> ohQuery = new LambdaQueryWrapper<OhRegister>();
        ohQuery.eq(OhRegister::getCompanyId, qo.getCompanyId()).eq(OhRegister::getIsSubmitted, 1);
        int overhaul = ohRegisterService.count(ohQuery);
        map.put("设备总数量", total);
        map.put("特种设备数量",special);
        map.put("报修设备数量", overhaul);
        return map;

    }

    public  Map getEquipStatusInfo(StatisticQO qo) {
        Map map = new HashMap<>();
        map.put(0, 0);
        map.put(1, 0);
        map.put(2, 0);
        map.put(3, 0);
        map.put(4, 0);
        map.put(5, 0);
        List<EqFacMng> equipList = getEquipList(qo);
        for (EqFacMng eqFacMng : equipList) {
            Integer equipStatus = eqFacMng.getEquipStatus();
            Integer lastNumber = (Integer) map.get(equipStatus);
            map.put(equipStatus, lastNumber + 1);
        }
        map.forEach((key, value) -> {
            String info = EquipStatus.statusType.get((Integer) key) + "," + value.toString();
            map.put(key, info);
        });
        return map;
    }

    public Map getEquipDepartmentInfo(StatisticQO qo) {
        Map map = new HashMap<>();
        List<EqFacMng> equipList = getEquipList(qo);
        for (EqFacMng eqFacMng : equipList) {
            Long mngCtrDeptId = eqFacMng.getMngCtrDeptId();
            map.putIfAbsent(mngCtrDeptId, 0);
            map.put(mngCtrDeptId, (Integer)map.get(mngCtrDeptId) + 1);
        }
        map.forEach((key,value) -> {
            Organization organization = organizationService.getById((Long)key);
            map.put(key, (organization == null ? "该部门已删除！" : organization.getName()) + "," + value.toString());
        });
        return map;
    }

    public Map getEquipTypeInfo(StatisticQO qo) {
        Map map = new HashMap<>();
        List<EqFacMng> equipList = getEquipList(qo);
        for (EqFacMng eqFacMng : equipList) {
            Long typeId = eqFacMng.getDeviceTypeId();
            map.putIfAbsent(typeId, 0);
            map.put(typeId, (Integer)map.get(typeId) + 1);
        }
        map.forEach((key, value) -> {
            DeviceType deviceType = deviceTypeService.getById((Long) key);
            map.put(key, (deviceType == null ? "该类型已删除！" : deviceType.getName()) + "," + value.toString());
        });
        return map;
    }

}
